package com.wd.health.view.fragment.wzysfragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.wd.health.R;
import com.wd.health.model.adapter.WzysAdapter;
import com.wd.health.model.bean.WzysBean;
import com.wd.health.model.bean.ZixunyishengBean;
import com.wd.health.utils.RetrofitManager;
import com.facebook.drawee.view.SimpleDraweeView;
import com.wd.health.view.activity.ConsultActivity;
import com.wd.health.view.activity.DoctorActivity;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class WzFragment05 extends Fragment {
    private SimpleDraweeView sdv_wzys;
    private TextView tv_wzys_name;
    private TextView tv_wzys_zw;
    private TextView tv_wzys_yy;
    private TextView tv_wzys_hpl;
    private TextView tv_wzys_hzs;
    private TextView tv_wzys_jiaqian;
    private TextView tv_wzys_ljzx;
    private RecyclerView rv_wzys;
    private TextView tv_wzys_zh;
    private TextView tv_wzys_hp;
    private TextView tv_wzys_zxs;
    private TextView tv_wzys_jg;
    private int id = 1;
    private WzysAdapter wzysAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_wz05, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        sdv_wzys = view.findViewById(R.id.sdv_wzys05);
        tv_wzys_name = view.findViewById(R.id.tv_wzys_name05);
        tv_wzys_zw = view.findViewById(R.id.tv_wzys_zw05);
        tv_wzys_yy = view.findViewById(R.id.tv_wzys_yy05);
        tv_wzys_hpl = view.findViewById(R.id.tv_wzys_hpl05);
        tv_wzys_hzs = view.findViewById(R.id.tv_wzys_hzs05);
        tv_wzys_jiaqian = view.findViewById(R.id.tv_wzys_jiaqian05);
        tv_wzys_ljzx = view.findViewById(R.id.tv_wzys_ljzx05);
        rv_wzys = view.findViewById(R.id.rv_wzys05);
        tv_wzys_zh = view.findViewById(R.id.tv_wzys_zh05);
        tv_wzys_hp = view.findViewById(R.id.tv_wzys_hp05);
        tv_wzys_zxs = view.findViewById(R.id.tv_wzys_zxs05);
        tv_wzys_jg = view.findViewById(R.id.tv_wzys_jg05);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false);
        rv_wzys.setLayoutManager(linearLayoutManager);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        initZh();
    }
    private void initZh() {
        tv_wzys_zh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_wzys_zh.setTextColor(Color.RED);
                tv_wzys_hp.setTextColor(Color.BLACK);
                tv_wzys_zxs.setTextColor(Color.BLACK);
                tv_wzys_jg.setTextColor(Color.BLACK);
                id = 1;
                initData();
            }
        });
        tv_wzys_hp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_wzys_zh.setTextColor(Color.BLACK);
                tv_wzys_hp.setTextColor(Color.RED);
                tv_wzys_zxs.setTextColor(Color.BLACK);
                tv_wzys_jg.setTextColor(Color.BLACK);
                id = 2;
                initData();
            }
        });
        tv_wzys_zxs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_wzys_zh.setTextColor(Color.BLACK);
                tv_wzys_hp.setTextColor(Color.BLACK);
                tv_wzys_zxs.setTextColor(Color.RED);
                tv_wzys_jg.setTextColor(Color.BLACK);
                id = 3;
                initData();
            }
        });
        tv_wzys_jg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_wzys_zh.setTextColor(Color.BLACK);
                tv_wzys_hp.setTextColor(Color.BLACK);
                tv_wzys_zxs.setTextColor(Color.BLACK);
                tv_wzys_jg.setTextColor(Color.RED);
                id = 4;
                initData();
            }
        });
    }
    private int mid = 0;
    private void initData() {

        RetrofitManager.getInstance().getWzysData(12, id, 1, 20, new Observer<WzysBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(WzysBean wzysBean) {
                List<WzysBean.ResultBean> list = wzysBean.getResult();
                wzysAdapter = new WzysAdapter(list, getContext());
                rv_wzys.setAdapter(wzysAdapter);
                sdv_wzys.setImageURI(list.get(0).getImagePic());
                tv_wzys_name.setText(list.get(0).getDoctorName());
                tv_wzys_zw.setText(list.get(0).getJobTitle());
                tv_wzys_yy.setText(list.get(0).getInauguralHospital());
                tv_wzys_hpl.setText("好评率"+list.get(0).getPraise());
                tv_wzys_hpl.setText("服务患者数"+list.get(0).getServerNum());
                tv_wzys_jiaqian.setText(list.get(0).getServicePrice()+"H币/次");
                initDocotrxq();
                wzysAdapter.setGetListener(new WzysAdapter.GetListener() {
                    @Override
                    public void onClick(final int doctorId, String imagePic, String doctorName, String jobTitle, String inauguralHospital, String praise, int serverNum, int servicePrice) {
                        sdv_wzys.setImageURI(imagePic);
                        tv_wzys_name.setText(doctorName);
                        tv_wzys_zw.setText(jobTitle);
                        tv_wzys_yy.setText(inauguralHospital);
                        tv_wzys_hpl.setText("好评率"+praise);
                        tv_wzys_hpl.setText("服务患者数"+serverNum);
                        tv_wzys_jiaqian.setText(servicePrice+"H币/次");
                        wzysAdapter.notifyDataSetChanged();
                        id = doctorId;
                        initDocotrxq();
                    }
                });
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
    private void initDocotrxq() {
        tv_wzys_ljzx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("TAG", "onClick: "+id);
                RetrofitManager.getInstance().putZixunData(id, new Observer<ZixunyishengBean>() {

                    private String ddd;

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ZixunyishengBean zixunyishengBean) {
                        String message = zixunyishengBean.getMessage();
                        if (message.equals("查询成功")){
                            Log.i("TAG", "咨询成功");
                            String doctorUserName = zixunyishengBean.getDoctorUserName();
                            Log.i("TAG", "onClick:"+doctorUserName);
                            Intent intent = new Intent(getContext(), ConsultActivity.class);
                            intent.putExtra("doctorUserName",doctorUserName);
                            intent.putExtra("id",id);
                            startActivity(intent);
                        }else if (message.equals("H币不足")){
                            Toast.makeText(getContext(), "H币不足", Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

            }
        });
        sdv_wzys.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), DoctorActivity.class);
                intent.putExtra("doctorId",mid);
                startActivity(intent);
            }
        });
    }
}